package com.safeluck.floatwindow.bean;

import android.text.TextUtils;
import android.util.Log;

import com.safeluck.floatwindow.MediaArgu;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @ProjectName: aaa
 * @Package: com.safeluck.floatwindow.bean
 * @ClassName: GlobalData
 * @Description: 存放全局数据，到处都可以调用；  单例
 * @Author: zhanwei.li
 * @CreateDate: 2021/8/7 10:13
 * @UpdateUser: 更新者
 * @UpdateDate: 2021/8/7 10:13
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class GlobalData {
    private static final String TAG = "GlobalData";
    //水印信息
    /*
    {
        "teacher": "张三",
            "student": "李四",
            "speed": 24.5,
            "longitude": "26.3231,
            "latitude": 109.3233,
            "school": "测试驾校",
            "car_license": "渝A1234学",
            "other": "",
    }

     */
    private String waterMaskInfo;
    private GlobalData() {
    }

    private static GlobalData instance;

    public static GlobalData getInstance() {
        if (instance == null) {
            synchronized (GlobalData.class) {
                if (instance == null) {

                    instance = new GlobalData();
                }
            }
        }

        return instance;

    }

    public void setWaterMaskInfo(String info){
        Log.i(TAG,"waterMaskInfo="+info);
        this.waterMaskInfo = info;
    }

    public String getWaterMaskInfo(){
        return waterMaskInfo;
    }

    public<T> T parseWaterMaskInfo(String key,T defaultValue,ShareType type){
        Object value = null;
        Log.i(TAG,"parseWaterMaskInfo="+key);
        if (!TextUtils.isEmpty(waterMaskInfo)){
            try {
                JSONObject jsonObject = new JSONObject(waterMaskInfo);
                switch (type){
                    case STRING:
                        value = jsonObject.getString(key);
                        break;
                    case INTEGER:
                        value = jsonObject.getInt(key);
                        break;
                    case LONG:
                        value = jsonObject.getLong(key);
                        break;
                    case DOUBLE:
                        value = jsonObject.getDouble(key);
                        break;
                    case BOOLEAN:
                        value = jsonObject.getBoolean(key);
                        break;
                }
                Log.i(TAG,"key="+key+" value="+value);
                return (T)value;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return defaultValue;
    }

    private MediaArgu mediaArgu;
    public MediaArgu getMediaArgu() {
        return mediaArgu;
    }
    public void setMediaArgu(MediaArgu mediaArgu){
        this.mediaArgu = mediaArgu;
    }

    public enum ShareType{
        STRING,INTEGER,LONG,DOUBLE,BOOLEAN
    }
}
